package com.ambulance.common.utils;

import com.ambulance.project.biz.dto.admin.PageDto;
import com.ambulance.utils.CommonUtils;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.google.common.base.CaseFormat;
import com.google.common.base.Function;
import com.google.common.collect.FluentIterable;
import com.google.common.collect.Lists;

import java.lang.reflect.Field;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 反射处理Bean，得到里面的属性值
 *
 * @author zhao
 */
public class ResultMapUtil {


    /**
     * 包装ipage
     *
     * @param iPage
     * @param fields
     * @return
     */
    public static PageDto customerObjFieldPage(IPage iPage, String... fields) {
        PageDto pageDto = new PageDto();
        pageDto.setTotal(iPage.getTotal());
        pageDto.setCurrent(iPage.getCurrent());
        pageDto.setSize(iPage.getSize());
        pageDto.setRecords(customerObjFields(iPage.getRecords(), fields));
        return pageDto;
    }


    /**
     * 指定显示字段并返回Map
     *
     * @param obj
     */
    public static Map<String, Object> customerObjField(Object obj, String... fields) {
        Map<String, Object> map = new HashMap<String, Object>();
        if (CommonUtils.checkNull(obj)) {
            return map;
        }
        ArrayList<String> list = Lists.newArrayList(fields);
        if (list.isEmpty()) {
            return map;
        }
        //转驼峰
        List<String> objFields = FluentIterable.from(list).transform(new Function<String, String>() {
            @Override
            public String apply(String s) {
                if (s.contains("_")) {
                    return CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.LOWER_CAMEL, s);
                } else {
                    return s;
                }
            }
        }).toList();
        // 得到类对象
        Class userCla = (Class) obj.getClass();
        /* 得到类中的所有属性集合 */
        Field[] fs = userCla.getDeclaredFields();
        for (int i = 0; i < fs.length; i++) {
            Field f = fs[i];
            f.setAccessible(true); // 设置些属性是可以访问的
            Object val = new Object();
            try {
                String name = f.getName();
                if (objFields.contains(name)) {
                    val = f.get(obj);
                    //判断val的类型是否为 LocalDateTime，若为则进行时间格式转换
                    if (val instanceof LocalDateTime) {
                        String localDateTime = CommonUtils.parseLocalDateTime((LocalDateTime) val);
                        map.put(name, localDateTime);
                    } else {
                        // 得到此属性的值
                        map.put(name, val);// 设置键值
                    }

                }

            } catch (IllegalArgumentException e) {
                System.out.println(e.getMessage());
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
                System.out.println(e.getMessage());
            }
        }
        return map;
    }

    /**
     * @param objectList 对象集合
     * @param fields     对象bean的字段
     * @return
     */
    public static List<Map<String, Object>> customerObjFields(List<?> objectList, String... fields) {
        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
        for (Object obj : objectList) {
            Map<String, Object> item = customerObjField(obj, fields);
            list.add(item);
        }
        return list;
    }


}
